// 根据权限控制 处理报警事务或人员管理用到的按钮（右上角） 的显示与否
function per_show(){
    let but = document.getElementById("showPopup");
    if (!(["人员名单", "异常数据表"].includes(document.title))) {
        return
    }
    if (window.staffpermission.split(",").includes("5") && document.title == "异常数据表") {
        let user = document.getElementById("user");
        user.style.right = "15%";
        but.style.display = "block";
    }
    if (window.staffpermission.split(",").includes("4") && document.title == "人员名单") {
        let user = document.getElementById("user");
        user.style.right = "15%";
        but.style.display = "block";
    }
}
// 根据权限调整菜单栏的展示情况
function nav_show(){
    let menu_li = document.querySelectorAll("#menu li");
    let per_li = window.staffpermission.split(",");
    let total_per_li = ["1","2","3","4","5"];
    let minu = total_per_li.filter(function(v){ return per_li.indexOf(v) == -1 });
    for (let i = 0; i < minu.length; i++) {
        switch (minu[i]) {
            case "1":
                menu_li[1].style.display = "none";
                break;
            case "2":
                menu_li[2].style.display = "none";
                break;
            case "3":
                menu_li[0].style.display = "none";
                break;
            case "4":
                menu_li[3].style.display = "none";
        }
        
    }
}
// 获取当前账户的个人信息（用户名，手机号，电子邮箱，权限）
function get_staff_data(){
    $.ajax({
        url: "/api/staff-data",
        type: "GET",
        dataType: "json",
        success: function(data) {
            var lis = document.querySelectorAll("#user li");
            for (let i = 1; i < 4; i++){
                lis[i - 1].insertAdjacentText("beforeend", data[i]);
            }
            window.staffpermission = data[4];
        }
    })
}

// 左上角的交互按钮的点击事件回调函数，用于打开菜单栏
function enter(){
    let nav_ele = document.querySelector("#menu");
    let nask = document.getElementById("mask");
    nav_ele.style.display == "none";
    nav_ele.style.animationName = "enter";
    nav_ele.style.display = "block";
    nask.style.display = "block";
}

// 弹出的菜单栏的最上边按钮的点击事件回调函数，用于关闭菜单栏
function leave(){
    let nav_ele = document.querySelector("#menu");;
    let nask = document.getElementById("mask");
    nav_ele.style.animationName = "leave";
    nask.style.display = "none";
    function x() {
        nav_ele.style.display = "none";
    }
    setTimeout(x, 500);
}

// 鼠标悬浮在右上角的人物头像，触发事件，这是对应的事件回调函数
function staff_show(){
    var nav_ele = document.querySelector("#user div");
    if (nav_ele.style.display == "none") {
        nav_ele.style.display = "block";
        nav_ele.style.backgroundColor = "white";
    } else{
        function x() {
            nav_ele.style.display = "none";
        }
        setTimeout(x, 500);
    }
}

// 右上角的退出按钮对应的点击事件回调函数
function exit(){
    // 构造要发送的数据
    let cook = document.cookie;
    let key = cook.split("=")[0];
    let value = cook.split("=")[1];
    let obj = {};
    obj[key] = value;
    // 通过XMLrequest发送post请求，请求删除数据库对应记录
    let url = "http://127.0.0.1:8880/api/del-session";
    let httpRequest = new XMLHttpRequest();
    httpRequest.open('POST', url, true);
    httpRequest.setRequestHeader("Content-type", "application/json");
    httpRequest.send(JSON.stringify(obj));
    // 请求成功的回调函数，刷新页面并删除cookie（通过设置有效期）
    httpRequest.onreadystatechange = function () {
        location.reload();
        document.cookie = "session_id" + '=;expires=Thu, 01 Jan 1970 00:00:01 GMT; path=/';
    };
}

// 调用函数，获取账户个人信息
get_staff_data();
